package com.yjht.system.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yjht.system.base.BaseServiceImpl;
import com.yjht.system.mapper.AdminRoleMapper;
import com.yjht.system.mapper.OrganizationMapper;
import com.yjht.system.mapper.RoleMapper;
import com.yjht.system.model.Admin;
import com.yjht.system.model.AdminRole;
import com.yjht.system.model.Organization;
import com.yjht.system.model.Role;
import com.yjht.system.service.IAdminService;
import com.yjht.system.vo.AdminVO;
import com.yjht.system.mapper.AdminMapper;
import net.dreamlu.tool.util.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lengleng
 * @since 2018-01-29
 */
@Service
public class AdminServiceImpl extends BaseServiceImpl<AdminMapper, Admin> implements IAdminService {
	@Autowired
	private OrganizationMapper organizationMapper;
	@Autowired
	private RoleMapper roleMapper;
	@Autowired
	private AdminRoleMapper adminRoleMapper;

	@Override
	public Admin findByName(String username) {
		Admin admin = new Admin();
		admin.setUsername(username);
		return selectOne(new EntityWrapper<>(admin));
	}

	@Override
	public void finalDataGrid(AdminVO adminVO, Page<AdminVO> pages) {
		Admin ewAdmin = BeanUtils.copy(adminVO, Admin.class);
		EntityWrapper<Admin> ew = new EntityWrapper<>(ewAdmin);

		LocalDateTime startTime = adminVO.getCreatedateStart();
		LocalDateTime endTime   = adminVO.getCreatedateEnd();
		ew.where(null != startTime, "create_time >= {0}", startTime);
		ew.where(null != endTime, "create_time <= {0}", endTime);

		List<Admin> list = baseMapper.selectPage(pages, ew);
		List<AdminVO> adminVOList = new ArrayList<AdminVO>();
		for (Admin admin : list) {
			AdminVO resultVo = BeanUtils.copy(admin, AdminVO.class);
			// 处理组织名
			Organization organization = organizationMapper.selectById(admin.getOrganizationId());
			adminVO.setOrganizationName(organization.getName());
			// 处理角色集合
			List<String> roleNameList = roleMapper.findListByAdminId(admin.getId())
				.stream()
				.map(Role::getName)
				.collect(Collectors.toList());
			resultVo.setRolesList(roleNameList);
			adminVOList.add(resultVo);
		}
		pages.setRecords(adminVOList);
	}

	@Override
	public boolean insertByVo(AdminVO adminVO) {
		Admin admin = BeanUtils.copy(adminVO, Admin.class);
		boolean r = this.insert(admin);

		Integer id = admin.getId();
		String[] roles = adminVO.getRoleIds().split(",");
		AdminRole adminRole = new AdminRole();
		for (String string : roles) {
			adminRole.setAdminId(id);
			adminRole.setRoleId(Integer.valueOf(string));
			adminRoleMapper.insert(adminRole);
		}
		return r;
	}

	@Override
	public boolean updateByVo(AdminVO adminVO) {
		Admin admin = BeanUtils.copy(adminVO, Admin.class);
		boolean r = this.updateById(admin);
		Integer id = adminVO.getId();

		AdminRole adminRole = new AdminRole();
		adminRole.setAdminId(id);
		List<AdminRole> adminRoles = adminRoleMapper.selectList(new EntityWrapper<>(adminRole));
		if (adminRoles != null && !adminRoles.isEmpty()) {
			for (AdminRole ewAdminRole : adminRoles) {
				adminRoleMapper.deleteById(ewAdminRole.getId());
			}
		}
		String[] roles = adminVO.getRoleIds().split(",");
		AdminRole dbAdminRole = new AdminRole();
		for (String string : roles) {
			dbAdminRole.setAdminId(id);
			dbAdminRole.setRoleId(Integer.valueOf(string));
			adminRoleMapper.insert(dbAdminRole);
		}
		return r;
	}
}
